import Vue from 'vue'
import VueRouter from 'vue-router'
import recommend from '../views/recommend.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: "/recommend"
  }, { // 推荐路由
    path: "/recommend",
    name: "recommend",
    component: recommend,
    meta: {
      title: '随身听-推荐'
    }
  },
  { // 推荐路由下的音乐列表组件
    path: "/recommend/musiclist",
    meta: {
      headerflag: true,
    },
    component: () => import('../components/recommend/musiclist.vue')
  },
  { // 歌手路由
    path: "/singer",
    name: "singer",
    component: () => import('../views/singer.vue'),
    meta: {
      title: '随身听-歌手'
    }
  },
  { // 歌手路由下的音乐列表组件
    path: "/singer/musiclist",
    meta: {
      headerflag: true
    },
    component: () => import('../components/singer/musiclist')
  },
  { // 排行榜下的音乐列表组件
    path: "/rank/musiclist",
    meta: {
      headerflag: true
    },
    component: () => import('../components/rank/musiclist')
  },
  { // 排行榜路由
    path: "/rank",
    name: "rank",
    component: () => import('../views/rank.vue'),
    meta: {
      title: '随身听-排行榜'
    }
  },
  {
    // mv路由
    path: '/mv',
    name: "mv",
    component: () => import('../views/mv.vue')
  },
  {
    path: '/search',
    name: 'search',
    meta: {
      headerflag: true
    },
    component: () => import('../views/search.vue')
  }, {
    path: '/login',
    name: 'login',
    meta: {
      headerflag: true
    },
    component: () => import('../views/login.vue')
  }, {
    path: "/lovemusic",
    name: "lovemusic",
    meta: {
      headerflag: true
    },
    component: () => import('../components/users/lovemusic.vue'),
  }, {
    path: "/about",
    name: "about",
    meta: {
      headerflag: true
    },
    component: () => import('../components/users/about.vue'),
  }, {
    path: "/userdetails",
    name: 'userdetails',
    meta: {
      headerflag: true
    },
    component: () => import('../views/user_details.vue'),
    // children: [{
    //   path: "/collect",
    //   name: "collect",
    //   meta: {
    //     headerflag: true
    //   },
    //   component: () => import('../components/users/collect.vue'),
    // }]
  },
  {
    path: "/collect",
    name: "collect",
    meta: {
      headerflag: true
    },
    component: () => import('../components/users/collect.vue'),
  },
]

const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  let login = store.state.loginflag || window.localStorage.getItem("login");


  // 登录界面
  if (to.path == "/login") {
    next();
    return;
  } else {
    // 判断登录情况
    if (!login) {
      next("/login");
      return;
    } else {
      next();
    }
  }
})

export default router